<?php
namespace App\Utils\Service\Ly;
use App\Common\Responses;
use Elasticsearch\ClientBuilder;
use Illuminate\Support\Facades\Log;
class EsServer{
    private static $client;
    private static $es;
    private function __construct()
    {
        //连接es
        $hosts = [
            config('es.host')    // HTTP Basic Authenticationt
        ];
        self::$client = ClientBuilder::create()
            ->setHosts($hosts)
            ->build();
    }

    private function __clone()
    {
        // TODO: Implement __clone() method.
    }

    //静态入口
    public static function EsIndex(){
        if (!self::$es instanceof EsServer){
            return self::$es = new EsServer();
        }
        return self::$es;
    }

    /**
     * 创建索引
     * indices 索引
     */
    public function createIndex()
    {
        $params = [
            'index' => 'ly_111',
            'body' => [
                'settings' => [
                    'number_of_shards' => 5,
                    'number_of_replicas' => 1
                ],
                'mappings' => [
                    '_source' => [
                        'enabled' => true
                    ],
                    'properties' => [
                        'fang_name' => [
                            'type' => 'text',//模糊搜索
                            'analyzer' => 'ik_max_word',//中文分词规则
                            'search_analyzer' => 'ik_max_word'//搜索使用中文分词
                        ],
                        'address'=>[

                            'type'=>'geo_point'
                        ]
                    ]

                ]
            ]
        ];
        try{
            return self::$client->indices()->create($params);
        }catch (\Exception $e)
        {
            return Responses::error([],0,$e->getMessage());
        }
    }
    /*
     * Es索引添加内容
     */
    public function addEs($index,$data=[]){
        $params = [
            'index' => $index,  //索引名
            'id'    => $data['id'], // id
            'body'  => $data  //要添加的数据
        ];
        $response = self::$client->index($params);
        //判断如果入库成功 存入日志
        if ($response){
            Log::info($data['id'] . '日志同步成功');
        }
    }

    public function update($index, $type, $id, $body)
    {
        $params = [
            'index' => $index,
            'type' => $type,
            'id' => $id,
            'body' => ['doc' => $body]
        ];

        return self::$client->update($params);
    }

    public function get($index, $type, $id) {
        $params = [
            'index' => $index,
            'type' => $type,
            'id' => $id
        ];
        return  self::$client->get($params);
    }

    /**
     * es搜索
     * @param $index
     * @param $type
     * @param $query
     * @return array|callable
     */

    public function search($index,$query,$sort)
    {

        $params = [
            'index' => $index, // 设置索引名称
            'body' => []
        ];
        if (!empty($query))  $params['body']['query'] =$query;
        if (!empty($sort)) $params['body']['sort'] =$sort;
      //  dd($params);
        return self::$client->search($params);
    }




}
